<?php

include_once ('bn_path.php');

class bn_rol
{
    private $files;
    private $dl;
    protected static $instance;

    function __construct()
    {
        $this->dl = new bn_dal();
        $this->files = new bn_directory();
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }


    public function insert($roleid, $url)
    {
        $this->dl->insert('rols', func_get_args());
    }

    public function delete($rolid, $url)
    {
        $this->dl->query('delete from rols where roleid=? and url=?', func_get_args());
    }

    public function update($url, $id) //
    {
        $this->dl->update('rols', array('url' => $url), array('id=' => $id));
    }

    private function rolid2rolname($rolid) //
    {
        if (is_numeric($rolid))
            return $rolid;
        else
            return $this->dl->select('select id from rolname where name=?', $rolid)->fetch();
    }

    public function List_ppage($rolname = '1') //
    {
        $path = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
        $filelist = array();
        $filelist[] = './';
        $filelist = array_merge($filelist, $this->files->get_dirlist(basepath, 'file','php'));

        $filelist[] = basepath . '/admin/';
        $filelist = array_merge($filelist, $this->files->get_dirlist(basepath .
            '/admin/', 'file', 'php'));

        $selected = $this->dl->select('select url from rols where roleid=?', self::
            rolid2rolname($rolname))->fetchCOL();

        $state = 0;
        $out = "<table>\n";
        for ($i = 0; $i < count($filelist); $i++)
        {
            $file = $filelist[$i];
            if (is_dir($file))
            {
                if ($state == 1)
                    $out .= "\n</fieldset>\n</td>\n</tr>";

                $dn = basename($file);
                if ($dn == '.')
                    $dn = 'root';
                $out .= "<tr>\n<td>\n<fieldset>\n<legend>" . $dn . "</legend>\n";
                $state = 1;
            } else
            {
                $is = false;
                foreach ($selected as $url)
                {
                    if ($path . $url == $file)
                    {
                        $is = true;
                        break;
                    }
                }
                if ($is)
                    $out .= "\n<input type='checkbox' value='" . $file . "' checked />" . basename($file) .
                        "<br>";
                else
                    $out .= "\n<input type='checkbox' value='" . $file . "'/>" . basename($file) .
                        "<br>";
            }
        }
        $out .= "\n</fieldset>\n</td>\n</tr>\n";
        $out .= "</table>";
        return $out;
    }

    public function List_vpage($rolname = '1') //
    {
        $list = $this->dl->select('select id,title from pages')->fetch();
        $out = '';
        if (isset($list))
        {
            $out .= "<table>\n";
            $out .= "<tr>\n";
            $out .= "\t<td>\n";
            $out .= "\t<fieldset>\n";
            $out .= "\t<legend>صفحات داخلی</legend>\n";
            $selected = $this->dl->select("SELECT url FROM rols where roleid=? and url like '" .
                root . "/page.php?pid=%'", array($rolname))->fetchCOL();
            foreach ($list as $lst)
            {
                $is = false;
                foreach ($selected as $url)
                {
                    if ($url == root . '/page.php?pid=' . $lst['id'])
                    {
                        $is = true;
                        break;
                    }
                }
                if ($is)
                    $out .= "\t<input type='checkbox' value='" . root . '/page.php?pid=' . $lst['id'] .
                        "' checked/>" . $lst['title'] . "<br>\n";
                else
                    $out .= "\t<input type='checkbox' value='" . root . '/page.php?pid=' . $lst['id'] .
                        "'/>" . $lst['title'] . "<br>\n";
            }
            $out .= "\t</fieldset>\n";
            $out .= "\t</td>\n";
            $out .= "</tr>\n";
            $out .= "</table>";
        }
        return $out;
    }

    public function list_modulepage($rolname = '1')
    {
        $path = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
        $out = "\n<table>";
        foreach ($this->files->get_dirlist(bn_get_dir_module(), 'dir') as $dir)
        {
            $basedir = basename($dir);

            $row = $this->dl->getrow('select name,id from modules where dir=?', $basedir)->
                fetch();
            $out .= "\n<tr>\n<td>\n<fieldset>\n<legend>" . $row['name'] . "_" . $row['id'] .
                "</legend>";
            $selected = $this->dl->select("SELECT url FROM rols where roleid=? and url like '" .
                root . "/modules/" . $basedir . "/%'", array($rolname))->fetchCOL();
            foreach ($this->files->get_dirlist($dir, 'file', 'php') as $file)
            {
                $is = false;
                foreach ($selected as $url)
                {
                    if ($path . $url == $file)
                    {
                        $is = true;
                        break;
                    }
                }
                if ($is)
                    $out .= "\t<input type='checkbox' value='" . $file . "' checked/>" . basename($file) .
                        "<br>\n";
                else
                    $out .= "\t<input type='checkbox' value='" . $file . "'/>" . basename($file) .
                        "<br>\n";
            }

            $out .= "\n</fieldset>\n<td>\n<tr>";
        }
        return $out . "\n</table>";
    }

    function __destruct()
    {
        unset($this->dl);
        unset($this->gf);
        unset($this->files);
    }
}

?>